package com.bigdata.controller;

import com.bigdata.model.*;
import com.bigdata.mysql.entity.JkfwMenu;
import com.bigdata.service.mysql.JkfwMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * JkfwMenu的路由接口服务
 *
 * @author
 */
@RestController
@RequestMapping(value = {"/mge/adx", "/mge/app"})
public class JkfwMenuController {

    @Autowired
    JkfwMenuService jkfwMenuService;

    @RequestMapping("listMenus")
    @ResponseBody
    public AjaxResultList listMenus(HttpServletRequest request, SearchModel searchModel) {
        Result res = jkfwMenuService.queryPage(searchModel);
        return AjaxResultList.page(res.getList());
    }

    /**
     * @Author lly
     * @param: request
     * @param: searchModel
     * @Date 2019/8/21 0021
     * @return: com.bigdata.vo.AjaxResult
     * @Description 获取查询树
     */
    @ResponseBody
    @RequestMapping("listTreeMenus")
    public AjaxResult listTreeMenus(HttpServletRequest request, SearchModel searchModel) {
        Result res = jkfwMenuService.queryGdTree(searchModel);
        return AjaxResultList.page(res.getList());
    }

    /**
     * @Author lly
     * @param: id
     * @Date 2019/10/28 0028
     * @return: com.bigdata.model.AjaxResult
     * @Description 获取机构树
     */
    @RequestMapping("getBymenuid")
    @ResponseBody
    public AjaxResult getByMenuId(String id) {
        JkfwMenu menu = jkfwMenuService.selectByPrimaryKey(id);
        if (menu == null) {
            return AjaxResult.fail("数据不存在！");
        } else {
            return AjaxResult.ok(menu);
        }
    }

    /**
     * @Author lly
     * @param: request
     * @param: id
     * @param: type
     * @Date 2019/10/11 0011
     * @return: com.bigdata.model.AjaxResult
     * @Description 获取功能树
     */
    @RequestMapping("treefunc")
    @ResponseBody
    public AjaxResult getTree(HttpServletRequest request, String id, String type) {
        List<ZTreeBean> trees = jkfwMenuService.getTreeBeans(request, id, type);
        return AjaxResult.ok(trees);
    }

    /**
     * @Author lly
     * @param: request
     * @param: id
     * @param: type
     * @Date 2019/10/28 0028
     * @return: com.bigdata.model.AjaxResult
     * @Description 用户模块
     */
    @RequestMapping("userFunc")
    @ResponseBody
    public AjaxResult userFunc(HttpServletRequest request, String id, String type) {
        List<ZTreeBean> trees = jkfwMenuService.userHomeTreeBeans(id, request, type);
        return AjaxResult.ok(trees);
    }

    @RequestMapping("addMenu")
    @ResponseBody
    public AjaxResult addMenu(JkfwMenu jkfwMenu) {
        Result res = jkfwMenuService.addMenu(jkfwMenu);
        return AjaxResult.ok(jkfwMenu.getMenuId());
    }

    @RequestMapping("updateMenu")
    @ResponseBody
    public AjaxResult updateMenu(JkfwMenu jkfwMenu) {
        if (jkfwMenu.getMenuId().equals(jkfwMenu.getParentId())) {
            return AjaxResult.fail("请不要上级菜单选择自己！");
        }
        jkfwMenuService.updateMenu(jkfwMenu);
        return AjaxResult.ok();
    }

    @RequestMapping("delMenu")
    @ResponseBody
    public AjaxResult delMenu(String id) {
        Result res = jkfwMenuService.delMenu(id);
        if (res.getCode() == Result.Code_Error) {
            return AjaxResult.fail(res.getMessage());
        }
        return AjaxResult.ok();
    }

    /**
     * @Author lly
     * @param: searchModel
     * @Date 2019/8/23 0023
     * @return: com.bigdata.vo.AjaxResult
     * @Description 查询菜单
     */
    @RequestMapping("searchMenu")
    @ResponseBody
    public AjaxResult searchMenu(SearchModel searchModel) {
        if (StringUtils.isNoneBlank(searchModel.getS_name())) {
            searchModel.setS_name(searchModel.getS_name().toLowerCase());
            Result res = jkfwMenuService.querySearch(searchModel);
            return AjaxResult.ok(res.getList());
        } else {
            return AjaxResult.ok();
        }
    }

}
